package demo.java.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.concurrent.ConcurrentHashMap;

import demo.java.util.TreeMap;

/**
 * Created by Administrator on 2017/4/27.
 */
public class TestCoreJava {
    public static void main(String[] args) {
        ConcurrentHashMap map = new ConcurrentHashMap();
        map.put("1","2");
        map.get("1");
        map.remove("1");
        TestListValue();
//        System.out.println(spread(1111));
//        TestHash(34,230);
//        TestMaxHashMap();
//        TestTreeMap();
//        TestLinkedList();

    }
    /*
     *       v
     * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+
     * | |->|A|->|B|->|C|->|D|->|E|->|F|->|G|->|H|->|I|->|J|->|K|->null
     * +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+  +-+
     * */
    private static void TestListValue(){
        Node[] list = new Node[10];
        Node n1 = new Node(1,2);
        list[0] = n1;
        Node n2 = list[0];
        n2.value = 3;
        System.out.println(list[0].value);
    }
    private static void TestMaxHashMap() {
        int i = 1 << 31 -1;
        int max = Integer.MAX_VALUE;
        System.out.println(i);
        System.out.println(max);
        //内存不够大，无法实例化过大的map
        HashMap<String,String> map = new HashMap<String,String>(1<<28);
//        for (long i = 0; i < Long.MAX_VALUE; i++) {
//            map.put("a"+i,i+"");
//            System.out.println(i);
//        }
    }

    private static void TestTreeMap() {
        TreeMap t = new TreeMap();
        t.put(2,2);
        t.put(4,4);
        t.put(1,1);
        t.put(3,3);
        t.put(5,5);
        t.put(6,6);
        t.put(7,7);
        t.put(8,8);
        t.put(9,9);
        TreeMap.Entry e = t.getEntry(1);
        e.printRoot();
        Iterator i =t.entrySet().iterator();
        while (i.hasNext()){
            i.next();
        }
    }

    private static void TestLinkedList() {
        LinkedHashMap l = new LinkedHashMap();
        l.put(9,9);
        l.put(2,1);
        l.put(1,3);
        l.put(3,3);
        l.put(7,7);
        l.put(4,4);
        l.put(6,6);
        l.put(5,5);
        l.put(8,8);
        Iterator i =l.entrySet().iterator();
        while (i.hasNext()){
            System.out.println(i.next());
        }
    }


    static final void TestHash(int n,int hash){
        int i = (n - 1) & hash;
        int y = hash%n;
        System.out.println(i+","+y);
    }


    static final int spread(int h) {
        return (h ^ (h >>> 16)) & 0x7FFFFFFF;
    }

    static class Node<K,V>{
        final K key;
        V value;
        Node(K key, V value) {
            this.key = key;
            this.value = value;
        }
    }
}
